package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.dao.StudentsDao;
import com.woniuxy.entity.*;
import com.woniuxy.service.StudentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class StudentsServiceImpl implements StudentsService {
    @Autowired(required = false)
    StudentsDao studentsDao;

    /**按照当前页和每页条数查询数据*/
    @Override
    public ResponseEntity findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage,pageSize);
        List<StudentsEntity> all = studentsDao.findAll();
        int i =currentPage*pageSize-(pageSize-1);
        for (StudentsEntity studentsEntity:all) {
            studentsEntity.setFakeId(i++);
        }
        PageInfo<StudentsEntity> pageInfo = PageInfo.of(all);
        return new ResponseEntity<>(200,"查询成功",pageInfo);
    }
    /**根据id，删除试卷菜单中的指定类别*/
    @Override
    public ResponseEntity deleteById(int id) {
        int i = studentsDao.deleteById(id);
        if (i > 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }
    /**批量删除*/
    @Override
    public ResponseEntity batchDelete(StudentsEntity studentsEntity) {
        int i = studentsDao.batchDelete(studentsEntity);
        if (i > 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }
    /**添加学生信息*/
    @Override
    public ResponseEntity insertStudent(StudentsEntity studentsEntity) {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        studentsEntity.setCreate_time(sf.format(date));
        studentsEntity.setUpdate_time(sf.format(date));
        int i = studentsDao.insertStudent(studentsEntity);
        if (i > 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }
    /**修改之前进行查询*/
    @Override
    public ResponseEntity updateBefore(StudentsEntity studentsEntity) {
        StudentsEntity updateBeforeStudent = studentsDao.updateBefore(studentsEntity);
        System.out.println("updateBefore"+updateBeforeStudent.getId());
        if (updateBeforeStudent != null) {
            return new ResponseEntity<>(200,"success", updateBeforeStudent);
        }
        return ResponseEntity.FAIL;
    }

    /**修改学生信息*/
    @Override
    public ResponseEntity updateStudent(StudentsEntity studentsEntity) {
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        studentsEntity.setUpdate_time(sf.format(date));
        int i = studentsDao.updateStudent(studentsEntity);
        if (i > 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }
    /**   按试卷分类名称搜索*/
    @Override
    public ResponseEntity searchStudent(StudentsEntity studentsEntity) {
        int currentPage=studentsEntity.getCurrentPage();
        int pageSize=studentsEntity.getPageSize();
        PageHelper.startPage(currentPage,pageSize);
        List<StudentsEntity> studentlist = studentsDao.searchStudent(studentsEntity);
        //System.out.println("asds"+studentlist);
        int i =currentPage*pageSize-(pageSize-1);
        for (StudentsEntity studentEntity:studentlist) {
            studentEntity.setFakeId(i++);
        }
        PageInfo<StudentsEntity > pageInfo = PageInfo.of(studentlist);
        if (studentlist != null && studentlist.size() > 0) {

            return new ResponseEntity<>(200,"success", pageInfo);
        }
        return ResponseEntity.FAIL;
    }

    @Override
    public ResponseEntity findErecord(int studentId) {
        List<ExamineeSum> allEids = studentsDao.findErecord(studentId);
        int i=1;
        for (ExamineeSum examineeSum:allEids) {
            examineeSum.setFakeId(i++);
            if (examineeSum.getIsExam()==0) {
                examineeSum.setState("未参加");
            }else {
                examineeSum.setState("已完成");
            }
        }
        return new ResponseEntity<>(200,"查询成功",allEids);
    }


}
